package com.koron.standard.jobStock.experiment.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.koron.standard.deviceClasssify.service.TypeService;
import com.koron.standard.jobStock.excel.JobStockListener;
import com.koron.standard.jobStock.experiment.bean.dto.ExperimentDTO;
import com.koron.standard.jobStock.experiment.bean.dto.ExperimentItemDTO;
import com.koron.standard.jobStock.experiment.bean.po.ExperimentItemParams;
import com.koron.standard.jobStock.experiment.bean.po.ExperimentParams;
import com.koron.standard.jobStock.experiment.service.ExperimentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jzp
 * @since 2022/2/23
 */
@Slf4j
public class ExperimentListener extends JobStockListener {

    private ExperimentService experimentService;

    private ExperimentDTO experimentDTO;
    private boolean isItem;

    public ExperimentListener(TypeService typeService, ExperimentService experimentService) {
        super(typeService);
        this.experimentService = experimentService;
    }

    public String saveData() {
        if (experimentDTO != null) {
            experimentService.saveDto(experimentDTO);
            return experimentDTO.getId();
        }
        return null;
    }

    public void resetData() {
        isItem = false;
        experimentDTO = new ExperimentDTO();
        experimentDTO.setItemDTOs(new ArrayList<>());
    }

    @Override
    public String templateSheetName() {
        return "试验标准模板";
    }

    @Override
    public void invoke(Map<Integer, String> map, AnalysisContext analysisContext) {
        if (isSkipped()) {
            return;
        }

        if ("作业项目".equals(map.get(0))) {
            isItem = true;
            return;
        }
        if ("作业标准名称".equals(map.get(0))) {
            // 作业标准名称
            if (StringUtils.isEmpty(map.get(2))) {
                setErrorMessage(getCurrentSheetName()+ "的作业标准名称不能为空");
                throw new ExcelAnalysisStopException();
            }
            experimentDTO.setName(map.get(2));
            // 设备分类ID
            if (StringUtils.isEmpty(map.get(4))) {
                setErrorMessage(getCurrentSheetName()+ "的设备分类ID不能为空");
                throw new ExcelAnalysisStopException();
            }
            experimentDTO.setTypeId(map.get(4));
            // 作业步骤层数
            experimentDTO.setStepLevel(Integer.valueOf(map.get(6)));
            experimentDTO.setAppid(getAppId());
            experimentDTO.setStatus(0);
            experimentDTO.setPublicStatus(0);
        } else if ("环境参数".equals(map.get(0))) {
            // 环境参数
            experimentDTO.setEnvParams(resolveParams(map.get(2), "environment"));
            // 设备参数
            experimentDTO.setDeviceParams(resolveParams(map.get(4), "equipment"));
        } else if (isItem && map.get(1) != null) {
            // 没有.则在第一层
            if (!map.get(1).contains(".")) {
                // 加入最外层
                ExperimentItemDTO experimentItemDTO = getByMap(map);
                if (experimentDTO.getStepLevel() > 1) {
                    experimentItemDTO.setChildren(new ArrayList<>());
                }
                experimentDTO.getItemDTOs().add(experimentItemDTO);
            } else {
                // 有.则在第二层
                List<ExperimentItemDTO> itemDTOs = experimentDTO.getItemDTOs();
                // 获取最后一层
                ExperimentItemDTO itemDTO = itemDTOs.get(itemDTOs.size() - 1);
                if (CollectionUtils.isEmpty(itemDTO.getChildren())) {
                    itemDTO.setChildren(new ArrayList<>());
                }
                itemDTO.getChildren().add(getByMap(map));
            }
        }

    }

    private List<ExperimentParams> resolveParams(String str, String type) {
        List<String[]> strings = resolveItemStr(str, ",|，");
        return strings.stream().map(e -> {
            ExperimentParams experimentParams = new ExperimentParams();
            experimentParams.setType(type);
            experimentParams.setName(e[0]);
            experimentParams.setUnit(e[1]);
            return experimentParams;
        }).collect(Collectors.toList());
    }

    private List<ExperimentItemParams> resolveItemParams(String str) {
        List<String[]> strings = resolveItemStr(str, "\n");
        return strings.stream().map(e -> {
            ExperimentItemParams experimentItemParams = new ExperimentItemParams();
            experimentItemParams.setName(e[0]);
            experimentItemParams.setUnit(e[1]);
            return experimentItemParams;
        }).collect(Collectors.toList());
    }

    private ExperimentItemDTO getByMap(Map<Integer, String> map) {
        ExperimentItemDTO experimentItemDTO = new ExperimentItemDTO();
        experimentItemDTO.setContext(map.get(2));
        experimentItemDTO.setContextStandard(map.get(3));
        experimentItemDTO.setRisk(map.get(4));
        experimentItemDTO.setLevel(getLevel(map.get(6)));
        experimentItemDTO.setItemParams(resolveItemParams(map.get(5)));
        return experimentItemDTO;
    }

}
